use std::fs;
use datafusion::prelude::*;

#[tokio::main]
async fn main() -> datafusion::error::Result<()> {
    // 不带列头的数据文件
    let csv_path = "order_table_without_head.csv";
    println!("{:?}", csv_path);

    // register the table
    let ctx = SessionContext::new();

    let mut options = CsvReadOptions::new();
    options = options.has_header(false);

    ctx.register_csv("order", csv_path, options).await?;

    // create a plan to run a SQL query
    let sql = getSQL();

    let df = ctx.sql(&sql).await?;

    // execute and print results
    df.show().await?;

    let record_batch_vec = df.collect().await?;
    let count: usize = record_batch_vec
        .iter()
        .map(|v| v.num_rows())
        .sum();
    println!("query count: {}", count);

    Ok(())
}

fn getSQL() -> String {
    let sql;
    {
        sql = "SELECT * FROM order ORDER BY column_1 desc LIMIT 10";
    }
    // {
    //     sql = "select * from order limit 5";
    // }
    // {
    //     sql = "select * from order \
    // union all select * from order \
    // union all select * from order \
    // union all select * from order \
    // union all select * from order ";
    // }

    sql.to_string()
}